package com.saoft.jcms.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.saoft.jcms.entity.Archives;
import com.saoft.jcms.service.ArchivesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class IndexController {

    @Autowired
    ArchivesService archivesService;

    @GetMapping(value = {"","/","/index"})
    public String index(Model model,
                        @RequestParam(value = "page",defaultValue = "1") Integer page,
                        @RequestParam(value = "limit",defaultValue = "12") Integer limit){
        Page qpage = new Page();
        qpage.setSize(limit);
        qpage.setCurrent(page);
        Page page1 = archivesService.page(qpage);
        model.addAttribute("page", page1);
        model.addAttribute("list", page1);
        return "index";
    }


    @GetMapping("search")
    public String search(String search,Model model,
                         @RequestParam(value = "page",defaultValue = "1") Integer page,
                         @RequestParam(value = "limit",defaultValue = "12") Integer limit
                         ){
        Page qpage = new Page();
        qpage.setSize(limit);
        qpage.setCurrent(page);

        Page page1 = archivesService.search(qpage,search);
        model.addAttribute("page", page1);
        model.addAttribute("list", page1);
        model.addAttribute("search", search);
        return "index";
    }

    @GetMapping("detail")
    public String detail(String id, Model model) {
        Archives archives = archivesService.getById(id);
        model.addAttribute("item", archives);
        return "detail";
    }

}
